package com.longcai.controller.nearby;


import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.nearby.ClyjNearbyShops;
import com.longcai.service.nearby.ClyjNearbyShopsService;

/**
 * <p>
 * 附近商铺 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-03-26
 */
@Controller
@RequestMapping("/clyjNearbyShops")
public class ClyjNearbyShopsController {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ClyjNearbyShopsService clyjNearbyShopsService;


    /**
     * 查询附近页面分类
     *
     * @return
     */
    @RequestMapping("/getShopClass")
    @ResponseBody
    public ApiResult getShopClass() {

        return this.clyjNearbyShopsService.getShopClass();
    }


    /**
     * 获取附近首页分类子类
     *
     * @param params
     * @return
     */
    @RequestMapping
    @ResponseBody
    public ApiResult getShopsClass(@RequestBody JSONObject params) {
        String shopsclass = params.getString("shopsclass");
        if (shopsclass.equals("") || shopsclass == null) {
            logger.error("获取失败+++++++++++++", shopsclass);
            return ApiResult.fail("获取失败");
        }
        return this.clyjNearbyShopsService.getShopsClass(shopsclass);
    }

    /**
     * 查询附近店铺列表(根据销量排序，可用于排行榜)
     *
     * @param params
     * @return
     */
    @RequestMapping("/getShops")
    @ResponseBody
    public ApiResult getShops(@RequestBody JSONObject params) {
        String country = params.getString("country");
        if (country.equals("") || country == null) {
            logger.error("查询失败，请定位具体区/县");
            return ApiResult.fail("查询失败，请定位具体区/县");
        }
        return this.clyjNearbyShopsService.getShops(country);
    }

    /**
     * 查询附近店铺列表(根据距离排序)
     *
     * @param params
     * @return
     */
    @RequestMapping("/getShopsByAddress")
    @ResponseBody
    public ApiResult getShopsByAddress(@RequestBody JSONObject params) {
        //经度
        String lng = params.getString("lng");
        //纬度
        String lat = params.getString("lat");
        if (lng == null || lng.equals("") || lat == null || lat.equals("")) {
            logger.error("查询失败，请定位");
            return ApiResult.fail("查询失败，请定位");
        }
        String country = params.getString("country");
        if (country.equals("") || country == null) {
            logger.error("查询失败，请定位具体区/县");
            return ApiResult.fail("查询失败，请定位具体区/县");
        }
        //距离
        String distance = params.getString("distance");
        return this.clyjNearbyShopsService.getShopsByAddress(lng, lat, country,distance);
    }

    /**
     * 根据商圈查询店铺
     * @param params
     * @return
     */
    @RequestMapping("/getShopsByTradingArea")
    @ResponseBody
    public ApiResult getShopsByTradingArea(@RequestBody JSONObject params) {
        String tradingid = params.getString("tradingid");
        if (tradingid.equals("") || tradingid == null){
            logger.error("查询失败");
            return ApiResult.fail("查询失败");
        }
        return this.clyjNearbyShopsService.getShopByTradingArea(tradingid);
    }

    /**
     * 根据商铺类型分类
     * @param params
     * @return
     */
    @RequestMapping("/getShopsByShopType")
    @ResponseBody
    public ApiResult getShopsByShopType(@RequestBody JSONObject params) {
        String shopType = params.getString("shopType");
        if(shopType == null || shopType.equals("")){
            logger.error("查询失败");
            return ApiResult.fail("查询失败");
        }
        return this.clyjNearbyShopsService.getShopsByShopType(shopType);
    }

    /**
     * 查询附近店铺详情
     *
     * @param params
     * @returngetShopsByAddress
     */
    @RequestMapping("/getShopsInfo")
    @ResponseBody
    public ApiResult getShopsInfo(@RequestBody JSONObject params) {
        String shopsid = params.getString("shopsid");
        if (shopsid.equals("") || shopsid == null) {
            logger.error("查询失败");
            return ApiResult.fail("查询失败");
        }
        return this.clyjNearbyShopsService.getShopsInfo(shopsid);
    }


    /**
     * 附近店铺关键字搜索keyvalue
     *
     * @param params
     * @return
     */
    @RequestMapping("/searchShop")
    @ResponseBody
    public ApiResult searchShop(@RequestBody JSONObject params) {
        //搜索关键字（可以为店铺类型、店铺名称、商圈名）
        String keyvalue = params.getString("keyvalue");
        //经度
        String lng = params.getString("lng");
        //纬度
        String lat = params.getString("lat");
        //获取用户id
        String userId = params.getString("userId");
        if (keyvalue == null) {
            logger.error("关键字为null============", keyvalue);
            return ApiResult.fail("关键字为null");
        }
        if (lng == null || lng.equals("") || lat == null || lat.equals("")) {
            logger.error("查询失败，请定位");
            return ApiResult.fail("查询失败，请定位");
        }
        String country = params.getString("country");
        if (country.equals("") || country == null) {
            logger.error("查询失败，请定位具体区/县");
            return ApiResult.fail("查询失败，请定位具体区/县");
        }

        return this.clyjNearbyShopsService.searchShop(keyvalue, lng, lat, country, userId);
    }

    /**
     * 搜索附近显示候选词
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/getSearchNearkey")
    public Map<String, Object> getSearchNearkey(@RequestBody JSONObject params) {
        Map<String, Object> result = new HashMap<String, Object>();
        String keyvalue = params.getString("keyvalue");
        try {
            if (keyvalue == null || keyvalue.equals("")) {
                return result;
            }
            result = this.clyjNearbyShopsService.getSearchNearkey(keyvalue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 店铺入驻
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/addShops")
    public ApiResult<?>addShops(@RequestBody JSONObject params,MultipartFile aptitudePicture,MultipartFile corporateCardPic, MultipartFile corporateBankCardPic,MultipartFile headPicture,MultipartFile pictureIn,MultipartFile corporateCardPicBack,MultipartFile threeCardPic) {
    	QueryWrapper<ClyjNearbyShops> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("corporate_card", params.getString("corporateCard"));
    	//ClyjNearbyShops clyjNearbyShops = clyjNearbyShopsService.selectOne(queryWrapper);
    	int count = clyjNearbyShopsService.selectCount(queryWrapper);
    	if(count!=0){
    		boolean bo = clyjNearbyShopsService.delete(queryWrapper);
    	}
    	boolean b =clyjNearbyShopsService.addShops(params,aptitudePicture,corporateCardPic,corporateBankCardPic,headPicture,pictureIn,corporateCardPicBack,threeCardPic);
    	if (b) {
			return ApiResult.success("审核中");
		}
        return ApiResult.fail("注册失败");
    }

}

